package com.inputabc.ct.v1.ui.inputbox.component;

import com.inputabc.EzGUIFramework.util.alias.Ez;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

public class InputBoxTextArea extends JTextArea {
    private InputBoxPopupMenu inputBoxPopupMenu;
    private String placeholderText = "在此输入文本...";
    private boolean showingPlaceholder = true;
    private Color placeholderColor = new Color(180, 180, 180);
    private Color textColor = new Color(40, 44, 52);
    private Border originalBorder;
    private Border focusedBorder;
    
    {
        setName(this.getClass().getName());
        inputBoxPopupMenu = new InputBoxPopupMenu();
        originalBorder = getBorder();
    }
    
    public InputBoxTextArea() {
        Ez.setJPopupMenuForSwing(this, inputBoxPopupMenu);
        setLineWrap(true);
        setWrapStyleWord(true);
        
        // 设置边距
        setBorder(new EmptyBorder(8, 10, 8, 10));
        
        // 初始显示占位文本
        showPlaceholder();
        
        // 添加焦点监听器处理占位文本
        addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                if (showingPlaceholder) {
                    // 清除占位文本
                    setText("");
                    setForeground(textColor);
                    showingPlaceholder = false;
                }
                
                // 应用焦点边框效果
                if (focusedBorder != null) {
                    setBorder(focusedBorder);
                }
            }
            
            @Override
            public void focusLost(FocusEvent e) {
                if (getText().trim().isEmpty()) {
                    // 显示占位文本
                    showPlaceholder();
                }
                
                // 恢复原始边框
                if (originalBorder != null) {
                    setBorder(originalBorder);
                }
            }
        });
    }
    
    /**
     * 显示占位文本
     */
    private void showPlaceholder() {
        setText(placeholderText);
        setForeground(placeholderColor);
        showingPlaceholder = true;
    }
    
    /**
     * 设置占位文本
     * @param text 占位文本
     */
    public void setPlaceholderText(String text) {
        this.placeholderText = text;
        if (showingPlaceholder) {
            showPlaceholder();
        }
    }
    
    /**
     * 设置占位文本颜色
     * @param color 占位文本颜色
     */
    public void setPlaceholderColor(Color color) {
        this.placeholderColor = color;
        if (showingPlaceholder) {
            setForeground(color);
        }
    }
    
    /**
     * 设置文本颜色
     */
    @Override
    public void setForeground(Color fg) {
        super.setForeground(fg);
        
        // 保存非占位符状态下的文本颜色
        if (!showingPlaceholder) {
            this.textColor = fg;
        }
    }
    
    /**
     * 获取文本，忽略占位文本
     */
    @Override
    public String getText() {
        String text = super.getText();
        return showingPlaceholder ? "" : text;
    }
    
    /**
     * 设置文本，处理占位文本逻辑
     */
    @Override
    public void setText(String t) {
        super.setText(t);
        
        // 如果设置空文本但不是在显示占位符的流程中，则显示占位符
        if ((t == null || t.isEmpty()) && !showingPlaceholder) {
            showPlaceholder();
        } else if (t != null && !t.isEmpty() && !t.equals(placeholderText)) {
            // 如果设置非空文本，且不是占位符，则清除占位符状态
            showingPlaceholder = false;
            setForeground(textColor);
        }
    }
    
    /**
     * 设置焦点边框
     * @param border 焦点状态下的边框
     */
    public void setFocusedBorder(Border border) {
        this.focusedBorder = border;
    }
    
    /**
     * 重写paint方法，添加平滑渲染
     */
    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        super.paintComponent(g2);
    }

    public InputBoxPopupMenu getInputBoxPopupMenu() {
        return inputBoxPopupMenu;
    }

    public void setInputBoxPopupMenu(InputBoxPopupMenu inputBoxPopupMenu) {
        this.inputBoxPopupMenu = inputBoxPopupMenu;
    }
}
